/**
 * Author:WH
 * 网络请求配置
 */
import axios from "axios";
import cookie from "../utils/cookie";
import {Toast} from 'antd-mobile';

require('../setupProxy');

// axios.defaults.timeout = 30000;
// axios.defaults.token = cookie.getCookie('token');
axios.defaults.baseURL = "https://statement.handsomewthewt.store/statement/api/";
axios.defaults.withCredentials = false;

// eslint-disable-next-line no-unused-vars
const headers = {
    "Content-Type": "application/json",
    'token': cookie.getCookie('token')
};

// eslint-disable-next-line no-unused-vars
const multipartHeaders = {
    "Content-Type": "multipart/form-data",
    'token': cookie.getCookie('token')
};


/**
 * http request 拦截器
 */
axios.interceptors.request.use(
        (config) => {
            console.log('[请求token]', cookie.getCookie('token'));
            console.log('[请求路径]', config.baseURL + config.url);
            return config;
        },
        (error) => {
            return Promise.reject(error);
        }
);

/**
 * http response 拦截器
 */
axios.interceptors.response.use(
        (response) => {
            console.log('[响应数据]:', response['data']);
            return response;
        },
        (error) => {
            console.log("请求出错：", error);
        }
);

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
        }).then((response) => {
            landing(url, params, response.data);
            resolve(response.data);
        })
                .catch((error) => {
                    reject(error);
                });
    });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

function post(url, data) {
    let Obj = {body: data};
    return new Promise((resolve, reject) => {
        axios.post(url, Obj, {
            headers: {
                "Content-Type": "application/json",
                'token': cookie.getCookie('token')
            },
        }).then(
                (response) => {
                    if (response.data.code === 2000) {
                        resolve(response.data);
                    } else {
                        msg(response)
                    }
                },
                (err) => {
                    reject(err);
                }
        );
    });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data).then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    msg(err);
                    reject(err);
                }
        );
    });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

function put(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                "Content-Type": "multipart/form-data",
                'token': cookie.getCookie('token')
            },
        }).then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    msg(err);
                    reject(err);
                }
        );
    });
}

/**
 * 统一接口处理，返回数据
 */
export default function (fecth, url, param) {
    // let _data = "";
    return new Promise((resolve, reject) => {
        switch (fecth) {
            case "get":
                console.log("begin a get request,and url:", url);
                get(url, param)
                        .then((response) => {
                            resolve(response);
                        })
                        .catch((error) => {
                            console.log("get request GET failed.", error);
                            reject(error);
                        });
                break;
            case "post":
                post(url, param)
                        .then((response) => {
                            resolve(response);
                        })
                        .catch((error) => {
                            console.log("get request POST failed.", error);
                            reject(error);
                        });
                break;
            case "patch":
                patch(url, param)
                        .then((response) => {
                            resolve(response);
                        })
                        .catch((error) => {
                            console.log("get request POST failed.", error);
                            reject(error);
                        });
                break;
            case "put":
                put(url, param)
                        .then((response) => {
                            resolve(response);
                        })
                        .catch((error) => {
                            console.log("get request POST failed.", error);
                            reject(error);
                        });
                break;
            default:
                break;
        }
    });
}

/**
 * 失败提示
 * */
function msg(err) {
    if (err && err.data) {
        switch (err.data.code) {
            case 4000:
                Toast.offline('错误代码：' + err.data.code + '\n错误信息:' + err.data.msg);
                // alert(err.data.code + ':' + err.data.msg);
                break;
            case 401:
                Toast.offline('错误代码：' + err.data.code + '\n错误信息:' + err.data.msg);
                // alert("未授权，请登录");
                break;

            case 403:
                Toast.offline('错误代码：' + err.data.code + '\n错误信息:' + err.data.msg);
                // alert("拒绝访问");
                break;

            case 404:
                Toast.offline('错误代码：' + err.data.code + '\n错误信息:' + err.data.msg);
                // alert("请求地址出错");
                break;

            case 408:
                alert("请求超时");
                break;

            case 500:
                alert("服务器内部错误");
                break;

            case 501:
                alert("服务未实现");
                break;

            case 502:
                alert("网关错误");
                break;

            case 503:
                alert("服务不可用");
                break;

            case 504:
                alert("网关超时");
                break;

            case 505:
                alert("HTTP版本不受支持");
                break;
            default:
        }
    }
}

/**
 * 查看返回的数据
 * @param url
 * @param params
 * @param data
 */
function landing(url, params, data) {
    if (data.code === -1) {
    }
}

